package com.example.oneSpringMVC.controller;


import com.example.oneSpringMVC.common.PageResult;
import com.example.oneSpringMVC.common.Result;
import com.example.oneSpringMVC.dto.RoleDto;
import com.example.oneSpringMVC.entity.Role;
import com.example.oneSpringMVC.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xinTong
 * @since 2024-01-24
 */
@RestController
@RequestMapping("api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色分页查询
     * @param pageQuery
     * @return
     */
    @GetMapping("list")
    public Result list(@RequestParam(value = "pageNum")int pageNum,
                       @RequestParam(value = "pageSize")int pageSize){
        PageResult result = roleService.list(pageNum, pageSize);
        return Result.success(result);
    }

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("listAll")
    public Result listAll(){
        List<Role> list = roleService.listAll();
        return Result.success(list);
    }

    /**
     * 删除角色信息
     * @param roleId
     * @return
     */
    @DeleteMapping("delete/{roleId}")
    public Result delete(@PathVariable Long roleId){
        roleService.delete(roleId);
        return Result.success();
    }

    @GetMapping("info/{roleId}")
    public Result info(@PathVariable Long roleId){
        Role role = roleService.info(roleId);
        return Result.success(role);
    }

    @GetMapping("query/{employeeId}")
    public Result query(@PathVariable Long employeeId){
        List<Role> list = roleService.query(employeeId);
        return Result.success(list);
    }

    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoleDto roleDto){
        roleService.saveOrUpdate(roleDto);
        return Result.success("保存或者更新成功",null);
    }
}
